var t = require("../../@babel/runtime/helpers/regeneratorRuntime"),
  e = require("../../@babel/runtime/helpers/asyncToGenerator"),
  a = require("../../@babel/runtime/helpers/createForOfIteratorHelper"),
  i = require("../../utils/http.js"),
  s = require("../../utils/api.js"),
  r = require("../../utils/util.js"),
  n = require("../../utils/language/switchLanguage.js");
Page({
  data: {
    list: [1, 2],
    dateList: [],
    isShowDate: !1,
    currentDate: "",
    currentTime: "",
    fileName: "",
    filePath: "",
    LetterfileName: "",
    LetterfilePath: "",
    TripfileName: "",
    TripfilePath: "",
    TourisefileName: "",
    TourisefilePath: "",
    explanationFileName: "",
    explanationFilePath: "",
    name: "",
    phone: "",
    Idcard: "",
    timeList: [],
    ticketList: [],
    orderId: "",
    remark: "",
    uploadUrl: "https://ticket.sxd.cn",
    _t: null,
  },
  onLoad: function (t) {
    var e = n.languageVersion(wx.getStorageSync("language"));
    this.setData({ _t: e, languageType: wx.getStorageSync("language") }),
      this.setData({
        type: t.type,
        orderId: t.orderId,
        merchantNo: wx.getStorageSync("merchantNo"),
      }),
      0 == t.type
        ? wx.setNavigationBarTitle({ title: e["学校预约"] })
        : 1 == t.type
          ? wx.setNavigationBarTitle({ title: e["旅行社预约"] })
          : wx.setNavigationBarTitle({ title: e["单位预约"] }),
      this.getTeamAppointmentDay({ type: t.type }),
      this.getProductList({
        orderType: 1,
        sourceType: 1,
        teamType: this.data.type,
      });
  },
  initLanguage: function () {
    var t = n.languageVersion(wx.getStorageSync("language"));
    this.setData({ _t: t, languageType: wx.getStorageSync("language") });
  },
  bindChangeDate: function (t) {
    this.data.list[t.detail.value].stop
      ? s.showMsg(this.data._t["当前日期为闭馆日！"])
      : this.data.list[t.detail.value].stopEntering
        ? s.showMsg(this.data._t["当前日期已停止入馆！"])
        : this.data.list[t.detail.value].reception
          ? s.showMsg(this.data._t["非接待日！"])
          : (this.setData({ currentDate: t.detail.value }),
            this.getTimeSlotsLimit());
  },
  bindChangeTime: function (t) {
    this.data.timeInterval[t.detail.value].choose
      ? this.setData({ currentTime: t.detail.value })
      : s.showMsg(this.data._t["当前时段不可选！"]);
  },
  chooseDate: function (t) {
    var e = t.currentTarget.dataset.idx;
    this.setData({ currentDate: e });
  },
  chooseTime: function () {
    s.showMsg(this.data._t["请先选择参观日期！"]);
  },
  datePicker: function () {
    this.setData({ isShowDate: !this.data.isShowDate });
  },
  getName: function (t) {
    this.setData({ name: t.detail.value });
  },
  getIdcard: function (t) {
    this.setData({ Idcard: t.detail.value });
  },
  getPhone: function (t) {
    this.setData({ phone: t.detail.value });
  },
  getRemark: function (t) {
    this.setData({ remark: t.detail.value });
  },
  getValue: function (t) {
    if (!(t.detail.value < 0 || "" == t.detail.value)) {
      var e = t.target.dataset.idx,
        a = this.data.ticketList;
      (a[e].number = parseInt(t.detail.value)), this.setData({ ticketList: a });
    }
  },
  addTicket: function (t) {
    var e = this.data.ticketList,
      a = t.currentTarget.dataset.idx;
    (e[a].number = e[a].number + 1), this.setData({ ticketList: e });
  },
  reduceTicket: function (t) {
    var e = this.data.ticketList,
      a = t.currentTarget.dataset.idx;
    0 != e[a].number &&
      ((e[a].number = e[a].number - 1), this.setData({ ticketList: e }));
  },
  realNameAuth: function (t, e, a) {
    i.realNameAuth(t, e).then(function (t) {
      if (void 0 !== a) {
        var e = { data: t.data, msg: t.msg };
        a(e);
      }
    });
  },
  CreateOrder: function () {
    var t = this;
    if ("" != this.data.currentDate)
      if ("" != this.data.currentTime)
        if ("" != this.data.filePath)
          if (1 == this.data.type || "" != this.data.LetterfilePath)
            if (1 != this.data.type || "" != this.data.TripfilePath)
              if (1 != this.data.type || "" != this.data.TourisefilePath)
                if ("" != this.data.name)
                  if ("" != this.data.Idcard)
                    if ("" != this.data.phone)
                      if (s.isMobile(this.data.phone)) {
                        var e = [
                          {
                            teamType: this.data.type,
                            merchantNo: this.data.merchantNo,
                            isExplanationServiceConsultation: 0,
                            contact: this.data.name,
                            contactNumber: this.data.phone,
                            notes: this.data.remark,
                            teamAppointmentItemParamList: [
                              {
                                planDay:
                                  this.data.list[this.data.currentDate].day,
                                timeSlotId:
                                  this.data.timeInterval[this.data.currentTime]
                                    .id,
                                visitorInformationFileUrl: this.data.filePath,
                              },
                            ],
                            application_file_url:
                              1 == this.data.type
                                ? this.data.TripfilePath
                                : this.data.LetterfilePath,
                            instructions_prove_fileUrl:
                              this.data.TourisefilePath,
                          },
                        ];
                        1 == this.data.type &&
                          (e[0].explanationFileUrl =
                            this.data.explanationFilePath),
                          console.log(e, "datadata");
                        var a = {
                            data: JSON.stringify(e),
                            customerIdCard: this.data.Idcard,
                            orderType: 1,
                            sourceType: 1,
                            payType: 4,
                            tradeType: "wxSmallPay",
                            payConfigType: 4,
                          },
                          r = s.encryptBASE64(JSON.stringify(a)),
                          n = { sign: s.autograph(r), signData: r };
                        i.CreateOrder(n).then(function (e) {
                          if (
                            (s.showMsg(t.data._t["提交成功"]),
                            wx.hideLoading(),
                            1 == e.orderStatus)
                          )
                            return (
                              wx.navigateTo({
                                url: "plugin-private://wx393d60d93f70599c/pages/order/index",
                              }),
                              void t.reset()
                            );
                          if (0 == e.orderStatus)
                            e.data
                              ? t.wechatPay(e.data)
                              : wx.navigateTo({
                                  url: "plugin-private://wx393d60d93f70599c/pages/order/index",
                                });
                          else e.data.qrcode;
                        });
                      } else s.showMsg(this.data._t["手机号码格式有误"]);
                    else s.showMsg(this.data._t["请输入联系电话"]);
                  else s.showMsg(this.data._t["请输入证件号码"]);
                else s.showMsg(this.data._t["请输入领队人员姓名"]);
              else s.showMsg(this.data._t["请上传导游证"]);
            else s.showMsg(this.data._t["请上传电子行程单"]);
          else s.showMsg(this.data._t["请上传介绍信"]);
        else s.showMsg(this.data._t["请上传参观名单"]);
      else s.showMsg(this.data._t["请选择参观时段"]);
    else s.showMsg(this.data._t["请选择参观日期"]);
  },
  wechatPay: function (t) {
    var e = this;
    requireMiniProgram()
      .requestPayment(t)
      .then(function (t) {
        if ("requestPayment:ok" == t.errMsg)
          return (
            wx.showLoading({ title: e.data._t["支付处理中"] + "..." }),
            void setTimeout(function () {
              wx.hideLoading(),
                wx.navigateTo({
                  url: "plugin-private://wx393d60d93f70599c/pages/order/index",
                }),
                this.reset();
            }, 1e3)
          );
      })
      .catch(function (t) {
        "requestPayment:fail cancel" != t.errMsg
          ? "requestPayment:fail cancel" != t.errMsg &&
            "requestPayment:fail" != t.errMsg &&
            wx.showModal({
              title: e.data._t["通知"],
              showCancel: !1,
              content: e.data._t["调起支付失败，请稍后重试"] + "...",
              confirmText: e.data._t["确认"],
            })
          : wx.navigateTo({
              url: "plugin-private://wx393d60d93f70599c/pages/order/index",
            });
      });
  },
  RecreateOrder: function () {
    var t = this;
    if ("" != this.data.currentDate)
      if ("" != this.data.currentTime)
        if ("" != this.data.filePath)
          if (1 == this.data.type || "" != this.data.LetterfilePath)
            if (1 != this.data.type || "" != this.data.TripfilePath)
              if (1 != this.data.type || "" != this.data.TourisefilePath)
                if ("" != this.data.name)
                  if ("" != this.data.Idcard)
                    if ("" != this.data.phone)
                      if (s.isMobile(this.data.phone)) {
                        var e = [
                          {
                            teamType: this.data.type,
                            merchantNo: this.data.merchantNo,
                            isExplanationServiceConsultation: 0,
                            contact: this.data.name,
                            contactNumber: this.data.phone,
                            teamAppointmentItemParamList: [
                              {
                                planDay:
                                  this.data.list[this.data.currentDate].day,
                                timeSlotId:
                                  this.data.timeInterval[this.data.currentTime]
                                    .id,
                                visitorInformationFileUrl: this.data.filePath,
                              },
                            ],
                            application_file_url:
                              1 == this.data.type
                                ? this.data.TripfilePath
                                : this.data.LetterfilePath,
                            instructions_prove_fileUrl:
                              this.data.TourisefilePath,
                          },
                        ];
                        1 == this.data.type &&
                          (e[0].explanationFileUrl =
                            this.data.explanationFilePath);
                        var a = {
                          data: JSON.stringify(e),
                          customerIdCard: this.data.Idcard,
                          orderType: 1,
                          sourceType: 1,
                          payType: 4,
                          tradeType: "wxSmallPay",
                          payConfigType: 4,
                          orderId: this.data.orderId,
                        };
                        i.updateTeamOrder(a).then(function (e) {
                          s.showMsg(t.data._t["提交成功"]),
                            t.reset(),
                            wx.setStorageSync("isRefresh", !0),
                            wx.navigateTo({
                              url: "plugin-private://wx393d60d93f70599c/pages/order/index",
                            });
                        });
                      } else s.showMsg(this.data._t["手机号码格式有误"]);
                    else s.showMsg(this.data._t["请输入联系电话"]);
                  else s.showMsg(this.data._t["请输入证件号码"]);
                else s.showMsg(this.data._t["请输入领队人员姓名"]);
              else s.showMsg(this.data._t["请上传导游证"]);
            else s.showMsg(this.data._t["请上传电子行程单"]);
          else s.showMsg(this.data._t["请上传介绍信"]);
        else s.showMsg(this.data._t["请上传参观名单"]);
      else s.showMsg(this.data._t["请选择参观时段"]);
    else s.showMsg(this.data._t["请选择参观日期"]);
  },
  reset: function () {
    var t,
      e = this.data.ticketList,
      i = a(e);
    try {
      for (i.s(); !(t = i.n()).done; ) {
        t.value.number = 0;
      }
    } catch (t) {
      i.e(t);
    } finally {
      i.f();
    }
    this.setData({
      currentDate: "",
      currentTime: "",
      fileName: "",
      filePath: "",
      LetterfileName: "",
      LetterfilePath: "",
      TripfileName: "",
      TripfilePath: "",
      TourisefileName: "",
      TourisefilePath: "",
      explanationFileName: "",
      explanationFilePath: "",
      name: "",
      phone: "",
      Idcard: "",
      remark: "",
      ticketList: e,
    });
  },
  uplodFileInstructions: function () {
    var a = this;
    return e(
      t().mark(function e() {
        var i;
        return t().wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (t.next = 2), requireMiniProgram().chooseMessageFile();
              case 2:
                (i = t.sent),
                  a.uploadInstructions(
                    i.tempFiles[0].path,
                    i.tempFiles[0].name,
                  ),
                  wx.showLoading({ title: a.data._t["上传中"] + "..." });
              case 5:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  uplodFileAplication: function () {
    var a = this;
    return e(
      t().mark(function e() {
        var i;
        return t().wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (t.next = 2), requireMiniProgram().chooseMessageFile();
              case 2:
                (i = t.sent),
                  a.uploadPdf(i.tempFiles[0].path, i.tempFiles[0].name),
                  wx.showLoading({ title: a.data._t["上传中"] + "..." });
              case 5:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  uplodFile: function () {
    var a = this;
    return e(
      t().mark(function e() {
        var i;
        return t().wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (
                  (t.next = 2), requireMiniProgram().chooseMessageFile("file")
                );
              case 2:
                (i = t.sent),
                  a.upLoad(i.tempFiles[0].path, i.tempFiles[0].name),
                  wx.showLoading({ title: a.data._t["上传中"] + "..." });
              case 5:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  upLoad: function (t, e) {
    var a = this;
    wx.uploadFile({
      url: ""
        .concat(r.Baseurl, "/uploadUserInfo?fileName=")
        .concat(e, "&teamType=")
        .concat(a.data.type),
      filePath: t,
      name: "file",
      header: {
        "Content-Type": "multipart/form-data",
        Authorization: wx.getStorageSync("token"),
      },
      formData: { method: "POST", fileName: e },
      success: function (t) {
        wx.hideLoading();
        var e = JSON.parse(t.data);
        200 != t.statusCode || 500 != e.code
          ? 200 == t.statusCode &&
            0 == e.code &&
            a.setData({ fileName: e.originalFilename, filePath: e.resPath })
          : wx.showModal({
              title: "",
              showCancel: !1,
              content: e.msg,
              confirmText: a.data._t["确认"],
            });
      },
      fail: function (t) {
        wx.hideLoading(), s.showMsg(this.data._t["上传失败"]);
      },
    });
  },
  uplodFileLetter: function (a) {
    var i = this;
    return e(
      t().mark(function e() {
        var a;
        return t().wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (t.next = 2), requireMiniProgram().chooseMessageFile();
              case 2:
                (a = t.sent),
                  i.PublicUpLoad(a.tempFiles[0].path, "letter"),
                  wx.showLoading({ title: i.data._t["上传中"] + "..." });
              case 5:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  uplodExplanation: function (a) {
    var i = this;
    return e(
      t().mark(function e() {
        var a;
        return t().wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (t.next = 2), requireMiniProgram().chooseMessageFile();
              case 2:
                if (
                  ((a = t.sent),
                  a.tempFiles[0].path.includes("docx") ||
                    a.tempFiles[0].path.includes("doc"))
                ) {
                  t.next = 7;
                  break;
                }
                return (
                  s.showMsg(i.data._t["请上传doc/docx文件"]), t.abrupt("return")
                );
              case 7:
                i.PublicUpLoad(a.tempFiles[0].path, "explanation"),
                  wx.showLoading({ title: i.data._t["上传中"] + "..." });
              case 9:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  uplodFileTrip: function (a) {
    var i = this;
    return e(
      t().mark(function e() {
        var a;
        return t().wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (t.next = 2), requireMiniProgram().chooseMessageFile();
              case 2:
                (a = t.sent),
                  i.PublicUpLoad(a.tempFiles[0].path, "trip"),
                  wx.showLoading({ title: i.data._t["上传中"] + "..." });
              case 5:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  uplodFileTourise: function (a) {
    var i = this;
    return e(
      t().mark(function e() {
        var a;
        return t().wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                (a = i),
                  wx.chooseMedia({
                    count: 1,
                    mediaType: ["image"],
                    sourceType: ["album", "camera"],
                    camera: "back",
                    success: function (t) {
                      a.PublicUpLoad(t.tempFiles[0].tempFilePath, "tourise"),
                        wx.showLoading({ title: a.data._t["上传中"] + "..." });
                    },
                  });
              case 2:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  PublicUpLoad: function (t, e) {
    var a = this;
    wx.uploadFile({
      url: "".concat(this.data.uploadUrl, "/api-cms/file/upload"),
      filePath: t,
      name: "file",
      header: {
        "Content-Type": "multipart/form-data",
        Authorization: wx.getStorageSync("token"),
      },
      formData: { method: "POST" },
      success: function (t) {
        wx.hideLoading();
        var i = JSON.parse(t.data);
        if (200 == t.statusCode && 0 == i.code) {
          if ("letter" == e)
            return void a.setData({
              LetterfileName: i.originalFilename,
              LetterfilePath: i.resPath,
            });
          if ("explanation" == e)
            return void a.setData({
              explanationFileName: i.originalFilename,
              explanationFilePath: i.resPath,
            });
          if ("trip" == e)
            return void a.setData({
              TripfileName: i.originalFilename,
              TripfilePath: i.resPath,
            });
          if ("tourise" == e)
            return void a.setData({
              TourisefileName: i.originalFilename,
              TourisefilePath: i.resPath,
            });
        }
      },
      fail: function (t) {
        wx.hideLoading(), s.showMsg(this.data._t["上传失败"]);
      },
    });
  },
  uploadImg: function () {
    var t = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sourceType: ["album", "camera"],
      camera: "back",
      success: function (e) {
        var a = e.tempFiles[0].tempFilePath;
        t.uploadOcr(a);
      },
    });
  },
  uploadOcr: function (t) {
    var e = this;
    wx.uploadFile({
      url: "".concat(r.Baseurl, "/uploadOcr"),
      filePath: t,
      name: "file",
      header: {
        "Content-Type": "multipart/form-data",
        Authorization: wx.getStorageSync("token"),
      },
      formData: { method: "POST" },
      success: function (t) {
        var a = JSON.parse(t.data);
        500 != a.code
          ? 0 == a.code &&
            e.setData({ name: a.data.name, Idcard: a.data.idNum })
          : s.showMsg(a.msg);
      },
    });
  },
  getTeamAppointmentDay: function (t) {
    var e = this;
    i.getTeamAppointmentDay(t).then(function (t) {
      var i,
        s = t.data,
        r = [],
        n = a(s);
      try {
        for (n.s(); !(i = n.n()).done; ) {
          var o = i.value,
            d = o.day + " " + o.week;
          r.push(d);
        }
      } catch (t) {
        n.e(t);
      } finally {
        n.f();
      }
      e.setData({ dateList: r, list: t.data }),
        e.getTimeSlotsLimit(t.data[0].day);
    });
  },
  getTimeSlotsLimit: function (t) {
    var e = this,
      s = {
        playDay:
          "" != this.data.currentDate
            ? this.data.list[this.data.currentDate].day
            : t,
        orderType: 1,
        sourceType: 1,
        teamType: this.data.type,
      };
    i.getTimeSlotsLimit(s).then(function (t) {
      var i,
        s = t.data,
        r = [],
        n = a(s);
      try {
        for (n.s(); !(i = n.n()).done; ) {
          var o = i.value,
            d = "".concat(o.startTime, "-").concat(o.endTime);
          r.push(d);
        }
      } catch (t) {
        n.e(t);
      } finally {
        n.f();
      }
      e.setData({ timeInterval: t.data, timeList: r });
    });
  },
  getModel: function () {
    if (this.data.currentDate) {
      var t = {
        merchantNo: this.data.merchantNo,
        teamType: this.data.type,
        playDay:
          "" != this.data.currentDate
            ? this.data.list[this.data.currentDate].day
            : date,
      };
      i.getModel(t).then(function (t) {
        wx.downloadFile({
          url: t.msg,
          success: function (t) {
            var e = t.tempFilePath;
            wx.openDocument({ filePath: e, fileType: "xlsx", showMenu: !0 });
          },
        });
      });
    } else wx.showToast({ title: "请先选择参观日期", icon: "none" });
  },
  getProductList: function (t) {
    var e = this;
    i.getProductList(t).then(function (t) {
      var i,
        s = t.data,
        r = a(s);
      try {
        for (r.s(); !(i = r.n()).done; ) {
          i.value.number = 0;
        }
      } catch (t) {
        r.e(t);
      } finally {
        r.f();
      }
      e.setData({ ticketList: s });
    });
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
});
